function myUrl(path)
{
	return baseurl+path;
}

function postReq(url, data)
{
	if (window.XMLHttpRequest)
	{
	  xmlHttp=new XMLHttpRequest();
	}
	else // for older IE 5/6
	{
	  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	final_url = myUrl(url);
	xmlHttp.open("POST",final_url,false);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	//http.setRequestHeader("Content-length", data.length);
	//http.setRequestHeader("Connection", "close");
	xmlHttp.send(data);
	var resp =  xmlHttp.responseText;
	return resp;
}

function getReq(url)
{
	if (window.XMLHttpRequest)
	  {
	  xmlHttp=new XMLHttpRequest();
	  }
	else // for older IE 5/6
	  {
	  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	var url= myUrl(url);
	xmlHttp.open("GET",url,false);
	xmlHttp.send();
	var resp = xmlHttp.responseText;
	return resp;
}

function redirect(url)
{
    location.replace(myUrl(url));
}

function voteArticleUp(id)
{
    document.getElementById("scoreInfoBlock_"+id).innerHTML=getReq("action/votearticleup/" + id);
   document.getElementById("boost_button").disabled = true;
}

function showFlagDialog()
{
 
	var flagdlg = document.getElementById("flagDlg");
	var h = - flagdlg.clientHeight;

    if(h < -flagdlg.offsetTop)
    {
        h = 50 - flagdlg.offsetTop;
    }
	var hh = h+"px";
      
	flagdlg.style.top = hh;

    //flagdlg.offsetTop = h;
	flagdlg.style.visibility = "visible";
}
function hideFlagDialog()
{
	var flagdlg = document.getElementById("flagDlg");
	flagdlg.style.visibility = "hidden";
}

function flagArticle(idarticle, flagtype)
{
	var desc = document.getElementById("flagExplanationText").value;
	var params = "flag_msg="+encodeURIComponent(desc);
	  
	var url= "action/flagarticle/" + idarticle + "/" + flagtype;
	
	hideFlagDialog();
	document.getElementById("flagLink").innerHTML=postReq(url,params); 
}

function actionDlgShow(flagID,nextEventId,nextEventText)
{
	var dlg = document.getElementById("actionDlg");
	var h = dlg.clientHeight;
	var hh = "-"+h+"px";
	dlg.style.top = hh;
	document.getElementById("actionDlgTitle").innerHTML=nextEventText;
	document.getElementById("actionText").value="";
	chosenNextEvent=nextEventId;
	actionFlagID=flagID;
	dlg.style.visibility = "visible";
}
function actionDlgHide()
{
	var dlg = document.getElementById("actionDlg");
	dlg.style.visibility = "hidden";
}
function responseAction(idarticle)
{
	var desc = document.getElementById("actionText").value;
	var params = "flag_msg="+encodeURIComponent(desc);
	  
	var url= "action/flagaction/" + idarticle + "/" + actionFlagID + "/" + chosenNextEvent;
	
	actionDlgHide();
	document.getElementById("actionOption"+ actionFlagID).innerHTML=postReq(url,params); 
}

function removeChildrenFromNode(node)
{
	while (node.hasChildNodes())
	{
	  node.removeChild(node.firstChild);
	}
}

function popup(dlg)
{
	var popup = document.getElementById("popupdlg");
	var created = false;
	if(popup == null)
	{
		created = true;
		popup = document.createElement('div');
		popup.setAttribute('id', 'popupdlg');
		popup.setAttribute('style', 'display:table; position:fixed; z-index:4; top:0; left:0; width:100%; height:100%; ');
	}
	else
	{
		removeChildrenFromNode( popup );
	}

	var dlgframe = document.createElement('div');
	dlgframe.setAttribute('style', 'display:table-cell; margin:0 auto; padding:10px 15px; vertical-align: middle;background-color:rgba(0,0,0,0.3); ');
	
	var dlgbody = document.createElement('span');
	dlgbody.setAttribute('style', 'display:block; margin:0 auto; background-color:#fff; width:40%; border: 1px solid black; padding: 3px;');
	dlgbody.innerHTML=dlg;
	dlgframe.appendChild(dlgbody);
	popup.appendChild(dlgframe);
	document.body.insertBefore(popup,document.body.firstChild);
}

function cancel_popup_dlg()
{
  var popup = document.getElementById("popupdlg");
  document.body.removeChild(popup);
}

function message_box(msg_text)
{
    var dlg = msg_text+'<input type=button value="OK" onclick="cancel_popup_dlg();"/>';
    popup(dlg);
}

function menu_expansion(menu_id)
{
    var menu = document.getElementById(menu_id);    
    var submenu = document.getElementById("sub" + menu_id);
    
    if(submenu.style.display != "none")
    {
        submenu.style.display = "none";
    }
    else
    {
        submenu.style.display = "block";
    }
}

function loginFromHere(uri)
{
	var params = "login_uri="+encodeURIComponent(uri);
	  
	var url= "login/fromp";
	
    window.location = postReq(url,params); 
}